package com.ewei.web.common.mybatis.handlers;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
/**
 * 扩展mybatis的baseTypeHandlre
 * @author David
 *
 */
public class TimeCastTypeHandler extends BaseTypeHandler<Long> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i,
			Long parameter, JdbcType jdbcType) throws SQLException {
		Timestamp timeStamp = new Timestamp(parameter);
		ps.setTimestamp(i, timeStamp);
	}

	@Override
	public Long getNullableResult(ResultSet rs, String columnName)
			throws SQLException {
		Timestamp timeStamp = rs.getTimestamp(columnName);
		if(null!=timeStamp){
			return timeStamp.getTime();
		}
		return null;
	}

	@Override
	public Long getNullableResult(ResultSet rs, int columnIndex)
			throws SQLException {
		Timestamp timeStamp = rs.getTimestamp(columnIndex);
		if(null!=timeStamp){
			return timeStamp.getTime();
		}
		return null;
	}

	@Override
	public Long getNullableResult(CallableStatement cs, int columnIndex)
			throws SQLException {
		Timestamp timeStamp = cs.getTimestamp(columnIndex);
		if(null!=timeStamp){
			return timeStamp.getTime();
		}
		return null;
	}
	
}
